package com.lyf.shop.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lyf.shop.entity.Product;
import com.lyf.shop.service.ProductService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (Product)表控制层
 *
 * @author makejava
 * @since 2021-11-30 15:56:31
 */
@Controller
@RequestMapping("product")
public class ProductController {
    /**
     * 服务对象
     */
    @Resource
    private ProductService productService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */

    @RequestMapping("toLookBook")
    public String toLookBook(Model model, int id) {
        //将id传入查出对应的数据
        Product product = productService.queryById(id);
        System.out.println(product);
        product.setUpdateTime(new Date());
        //将查出的数据放到book里
        model.addAttribute("product", product);
        return "jsp/details";
    }

    @RequestMapping("toDisplay")
    public String toDisplayArticle(Model model, int id) {
        //将id传入查出对应的数据
        Product product = productService.queryById(id);
        System.out.println(product);
        product.setUpdateTime(new Date());
        //将查出的数据放到book里
        model.addAttribute("product", product);
        return "jsp/index_article";
    }

    @GetMapping("selectOne")
    @ResponseBody
    public Product selectOne(Integer id) {
        return this.productService.queryById(id);
    }

    @RequestMapping("/test")
    public String test(){
        return "admin";
    }

    @RequestMapping("/a")
    public String selectOne(Model model) {
        System.out.println("=====");
        Product product = productService.queryById(1);
        System.out.println(product.getPid() + product.getPname());

        model.addAttribute("msg", product);
        System.out.println(product);
        return "farm";
    }
    //跳转到添加书籍的页面,只负责跳转到增加书籍
    @RequestMapping("/add")
    public String toAddProduct() {
        return "jsp/addBook";
    }

    //添加书籍的请求
    @RequestMapping("/addBook")//这个添加方法的入口
    public String toAddBook(Product product) {
        System.out.println("我进入了addBook方法");
        product.setUpdateTime(new Date());
        productService.insert(product);//传入参数
        System.out.println(product);
        return "redirect:/product/allBook";//重定向到allbook
    }


    @RequestMapping("/toUpdateBook")
    public String toUpdateBook(Model model, int id) {
        //将id传入查出对应的数据
        Product product = productService.queryById(id);
        System.out.println(product);
        //将查出的数据放到book里
        model.addAttribute("product", product);
        return "jsp/updateBook";
    }

    @RequestMapping("/updateBook")
    public String updateBook(Model model, Product product) {
        System.out.println(product);//查看数据有没有进来
        productService.update(product);//进行修改
        //把修改后的数据通过它的id查出对应的数据
        Product products = productService.queryById(product.getPid());
        //放到books里面
        model.addAttribute("products", products);
        return "redirect:/product/allBook";
    }


    @RequestMapping("toDeleteBook")
    public String toDeleteBook(Model model, int id) {
        //将id传入查出对应的数据
        Product product = productService.queryById(id);
        System.out.println(product);
        //将查出的数据放到book里
        model.addAttribute("product", product);
        return "jsp/deleteBook";
    }

    @RequestMapping("/deleteBook")
    public String deleteBook(int pid) {
        //进行删除操作
        productService.deleteById(pid);
        return "redirect:/product/allBook";
    }


    //在前端显示数据库的所有的数据，全部查询
    @RequestMapping("/allBook")//这个方法的路径
    public String list(Model model) {
        List<Product> list = productService.queryAll(new Product());
        System.out.println(new Product());
        Product product=new Product();
        product.setUpdateTime(new Date());
        model.addAttribute("list", list);
        return "jsp/allBook";//返回到前端页面上，由jsp负责接受方法获取的数据
    }
    // 查询所有的数据并且展示到前端
    @RequestMapping(value = "/allBook", method = RequestMethod.GET)
    public String pageList(ModelMap map, @RequestParam(defaultValue = "1", required = true, value = "pageNo") Integer pageNo) {
        //每页显示记录数
        Product product=new Product();
        Integer pageSize = 4;
        System.out.println(pageSize);
        //分页查询
        PageHelper.startPage(pageNo, pageSize);
        //获取所有用户信息
        List<Product> productList = productService.queryAll(product);
        System.out.println(productList);
        PageInfo<Product> pageInfo = new PageInfo<Product>(productList);
        map.addAttribute("pageInfo", pageInfo);
//        System.out.println("输出NO:" + pageNo + "输出Info：" + pageSize + "输出Info：" + pageInfo);
        return "jsp/allBook";
    }
}